package de.gameduell.framework.game.watch {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	import de.gameduell.framework.game.watch.SingleStopWatch;

	public class StopWatchView {
		
		protected var stopWatch:SingleStopWatch;
		protected var clock:Sprite;
		protected var quarter_1:Sprite;
		protected var quarter_2:Sprite;
		protected var quarter_3:Sprite;
		protected var quarter_4:Sprite;
		protected var mask_1:Sprite;
		protected var mask_2:Sprite;
		protected var mask_3:Sprite;
		protected var mask_4:Sprite;
		protected var container:Sprite;

		public function StopWatchView(aClock:Sprite){
			container = aClock;					
			
			clock 	  = aClock.getChildByName("watch") as Sprite;
			quarter_1 = Sprite(clock.getChildByName("Quarter_1"));
			quarter_2 = Sprite(clock.getChildByName("Quarter_2"));
			quarter_3 = Sprite(clock.getChildByName("Quarter_3"));
			quarter_4 = Sprite(clock.getChildByName("Quarter_4"));
			mask_1 	  = Sprite(quarter_1.getChildByName("Mask_I"));
			mask_2 	  = Sprite(quarter_2.getChildByName("Mask_I"));
			mask_3 	  = Sprite(quarter_3.getChildByName("Mask_I"));
			mask_4 	  = Sprite(quarter_4.getChildByName("Mask_I"));
				
			stopWatch = SingleStopWatch.getInstance();
			stopWatch.addEventListener(StopWatch.TIMER_TICK, update);
		}
		
		public function set position(newPos:Point):void {
			container.x = newPos.x;
			container.y = newPos.y;
		}
		
		protected function update(event:Event = null):void{
			var currDegrees:Number = stopWatch.passedTimeDegree;
			mask_1.rotation = (currDegrees <  90)  ? currDegrees:90;
			mask_2.rotation = (currDegrees >  90)  ? ((currDegrees < 180) ? currDegrees-90  : 90):0;
			mask_3.rotation = (currDegrees > 180)  ? ((currDegrees < 270) ? currDegrees-180 : 90):0;
		    mask_4.rotation = (currDegrees > 270)  ? ((currDegrees < 360) ? currDegrees-270 : 90):0;
		}
		
		public function set visible(isVisible:Boolean):void {
			container.visible = isVisible;
		}
	}
}

